/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShowCuenta.java
 *
 * Created on Dec 13, 2011, 10:55:48 AM
 */

package INTERFAZ;
import CONTROLES.ControlSucursal;
import CONTROLES.ControlEmpleado;
import CONTROLES.ControlEmpleo;
import CONTROLES.ControlUsuario;
import MODELOS.Cliente;
import MODELOS.Empleado;
import MODELOS.Empleo;
import MODELOS.Usuario;
import UTILITARIOS.Herramienta;
import UTILITARIOS.MiModelo;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author VICENTE
 */
public class ListaEmpleado extends JDialog {

    /** Creates new form ShowCuenta */

    ControlSucursal control = new ControlSucursal();
    String lista[] = control.nombresSuc();
    ControlEmpleo cEmp = new ControlEmpleo();


    public ListaEmpleado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setAlwaysOnTop(false);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
       // this.cliente = c;
        initComponents();
        cambiarPropiedades();
        setResizable(false);
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        activas = new javax.swing.JTable();
        choice1 = new java.awt.Choice();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("LISTA DE EMPLEADOS");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                dispose(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ACCIONES", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jButton1.setText("Editar Empleado");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Agregar/Editar Usuario");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(91, 91, 91))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "EMPLEADOS", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 51, 102))); // NOI18N

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N

        jScrollPane2.setViewportView(activas);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1096, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        choice1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                choice1ItemStateChanged(evt);
            }
        });

        jLabel1.setText("Listar por Sucursal :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(272, 272, 272)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(412, 412, 412)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void choice1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_choice1ItemStateChanged

        //System.out.println("id :"+getSucursal());
        String idSuc = getSucursal();
        System.out.println(idSuc);
        StringTokenizer s = new StringTokenizer(idSuc,"-");
        int idSucursal = Integer.parseInt(s.nextToken());

        MiModelo modelo_activas = new MiModelo(new Object [][] {},new String [] {"ID","Nombre", "Apellidos","Nit", "Ciudad", "Teléfono", "Celular", "Dirección","Empleo","Estado"},-1);
        ControlEmpleado c_empleado = new ControlEmpleado();

        ArrayList<Empleado> empleados = new ArrayList<Empleado>();

        empleados = c_empleado.getEmpleadosSucursalSueldo(idSucursal);
        if(empleados.size()>0){
            for(int i=0;i<empleados.size();i++){
                Object [] auxEmpleado = {empleados.get(i).getIdEmpleado(),empleados.get(i).getNombreEmpleado(),empleados.get(i).getApellidoEmp(),empleados.get(i).getNitEmp(),empleados.get(i).getCiudadEmp(),empleados.get(i).getTelefonoEmp(),empleados.get(i).getCelularEmp(),empleados.get(i).getDireccionEmp(),cEmp.dameNomEmpleo(empleados.get(i).getIdEmpleo()),getEstado(empleados.get(i).isEstadoEmp())};
                modelo_activas.addRow(auxEmpleado);
            }
        }
        activas.setModel(modelo_activas);
        
        

    }//GEN-LAST:event_choice1ItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       
        String idSuc = getSucursal();
        if(activas.getValueAt(activas.getSelectedRow(), 0)!= null){
                    int x = ((Integer) activas.getValueAt(activas.getSelectedRow(), 0)).intValue();
//                    System.out.println(x+"=valor x");
                    Empleado c_1 = new ControlEmpleado().dameEmpleado(x);
                     EditarEmpleado emp = new EditarEmpleado();
                     java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd-MM-yyyy");
                    String fecha = sdf.format(c_1.getFechaNacimiento());
                    System.out.println("fecha: "+fecha);

                    emp.setIdEmpleado(c_1.getIdEmpleado());
                    emp.setEstado(c_1.isEstadoEmp());
                    emp.setGenero(c_1.getGeneroEmp());
                    emp.setTxtCiudadEmp(c_1.getCiudadEmp());
                    emp.setTxtDireccion(c_1.getDireccionEmp());
                    emp.setTxtEmail(c_1.getEmailEmp());
                    emp.setTxtNitEmpleado(String.valueOf(c_1.getNitEmp()));
                    emp.setTxtNombreEmpleado(c_1.getNombreEmpleado());
                    emp.settxtApellido(c_1.getApellidoEmp());
                    emp.setTxtTelfCasa(String.valueOf(c_1.getTelefonoEmp()));
                    emp.setTxtTelfMovil(String.valueOf(c_1.getCelularEmp()));
                    emp.setSucursal(idSuc);
                    emp.setFechaNacimiento(fecha);
                    emp.setEmpleo(c_1.getIdEmpleo()+"-"+cEmp.dameNomEmpleo(c_1.getIdEmpleo()));

                    
                    Herramienta.centerOnScreen(emp);
                            emp.setModal(true);
                            emp.setVisible(true);
                }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void dispose(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_dispose
        addWindowListener( new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				//JOptionPane.showMessageDialog(null, "Seguro que quiere salir", "Adventencia", JOptionPane.OK_OPTION);
                                //System.exit(0);
                            dispose();
                        }
		});
    }//GEN-LAST:event_dispose

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ControlUsuario c_usu = new ControlUsuario();

         if(activas.getValueAt(activas.getSelectedRow(), 0)!= null){

                    int x = ((Integer) activas.getValueAt(activas.getSelectedRow(), 0)).intValue();
                    ControlEmpleado c_emp = new ControlEmpleado();
                    Empleado e = new Empleado();
                    e = c_emp.dameEmpleado(x);
                    Usuario c_1 = new ControlUsuario().getUsuarioId(x);


                    if(c_usu.esUsuario(e.getIdEmpleado())){
                     EditarUsuario usu = new EditarUsuario();
                     java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd-MM-yyyy");
                    String fecha = sdf.format(c_1.getFechaCreacion());

                     usu.setChxEstado(c_1.isEstado());
                     usu.setIdUsuario(c_1.getIdUsuario());
                     usu.setTxtNombre(c_1.getNombreUsuario());
                     usu.setTxtTipoUsuario(c_1.getTipoUsuario());
                     usu.setTxtNombreEmp(e.getIdEmpleado()+"-"+e.getNombreEmpleado()+" "+e.getApellidoEmp());
                     usu.setTxtContrasena(c_1.getContrasena());
                     usu.setTxtConfContrasena(c_1.getContrasena());
                     usu.setFechaCreado(fecha);
                   
                    Herramienta.centerOnScreen(usu);
                            usu.setModal(true);
                            usu.setVisible(true);
                    }else{
                        int res = JOptionPane.showConfirmDialog(null, "El empleado no tiene asignado un Usuario, Aceptar para asignar", "Advertencia", JOptionPane.OK_CANCEL_OPTION);
                                if(res ==0){
                                 FormUsuario nU = new FormUsuario();
                                 Herramienta.centerOnScreen(nU);
                                 nU.setTxtNombreEmp(e.getIdEmpleado()+"-"+e.getNombreEmpleado()+" "+e.getApellidoEmp());
                                 nU.setModal(true);
                                 nU.setVisible(true);
                        }
                    }
           }

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    private void cambiarPropiedades(){
       
        MiModelo modelo_activas = new MiModelo(new Object [][] {},new String [] {"ID","Nombre", "Apellidos","Nit", "Ciudad", "Teléfono", "Celular", "Dirección","Empleo","Estado"},-1);
        ControlEmpleado c_empleado = new ControlEmpleado();

        ArrayList<Empleado> empleados = new ArrayList<Empleado>();
        for(int i=0; i<lista.length; i++){
        choice1.add(lista[i].toString());
        }

        choice1.select(Bienvenida.sucursal.getIdSucursal()+"-"+Bienvenida.sucursal.getNombreSuc());
        if(Bienvenida.user.getTipoUsuario().equalsIgnoreCase("ADMINISTRADOR") && Bienvenida.user.getToken().equalsIgnoreCase("INGRESAR") ){
       choice1.setEnabled(true);
       }else{
       choice1.setEnabled(false);
       }
        empleados = c_empleado.getEmpleadosSucursalSueldo(Bienvenida.sucursal.getIdSucursal());
        if(empleados.size()>0){
            for(int i=0;i<empleados.size();i++){
                Object [] auxEmpleado = {empleados.get(i).getIdEmpleado(),empleados.get(i).getNombreEmpleado(),empleados.get(i).getApellidoEmp(),empleados.get(i).getNitEmp(),empleados.get(i).getCiudadEmp(),empleados.get(i).getTelefonoEmp(),empleados.get(i).getCelularEmp(),empleados.get(i).getDireccionEmp(),cEmp.dameNomEmpleo(empleados.get(i).getIdEmpleo()),getEstado(empleados.get(i).isEstadoEmp())};
                modelo_activas.addRow(auxEmpleado);
            }
        }        
        activas.setModel(modelo_activas);        
               
    }

    public static void main(String args[]) {
        
        ListaEmpleado dialog = new ListaEmpleado(new javax.swing.JFrame(), true);
        dialog.setVisible(true);
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable activas;
    private java.awt.Choice choice1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private Cliente cliente;


    public String getSucursal()
    {
        return choice1.getSelectedItem();
    }

    public String getEstado(boolean est)
        {
           String res="";
            if(est == true)
                res = "Activo";
            else
                res = "Inactivo";
            return res;
        }

    
     
}






    

